"""
Case Type   : 资源池化
Case Name   : 系统函数query_page_distribution_info函数节点切换后验证
Create At   : 2024.6
Owner       : haomeng
Description :
    1、创建表，插入数据
    2、主节点查询数据页面的分布信息(数据文件)
    3、执行switchover
    4、新主节点查询页面分布信息
    5、恢复集群初始状态
Expect      :
    1、创建表成功，插入数据成功
    2、数据文件查看成功
    3、切换成功
    4、查询结果成功
    5、恢复集群初始状态成功，集群状态正常
"""


import os
import time
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.table = 't_ss_inner_06'
        self.e_msg1 = 'ERROR:  [SS] cannot query query_page_distribution_info' \
                      ' at Standby node while DMS enabled!'
        self.e_msg2 = 'INFO:  buffer does not exist'

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_innerfunc(self):
        text = '-----step1:创建表，插入数据; expect:创建成功，插入数据成功-----'
        self.log.info(text)

        cmd = f"create table {self.table}(id int);" \
              f"insert into {self.table} values(generate_series(0,10000));"
        self.log.info(cmd)
        cre_tab = self.pri_sh.execut_db_sql(cmd)
        self.log.info(cre_tab)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, cre_tab, '建表失败')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, cre_tab, '插入数据失败')

        text = '-----step2:多次查询表数据页面的分布信息(数据文件); expect:数据文件查看成功-----'
        self.log.info(text)

        sql1 = f'''select * from query_page_distribution_info('{self.table}',0,0);'''
        self.log.info(sql1)
        for i in range(10):
            res1 = self.pri_sh.execut_db_sql(sql1)
            self.log.info(res1)
            self.assertIn('(1 row)', res1, '查看表数据页面的分布信息成功')

        text = '-----step3:执行switchover操作使备1升主; expect:switchover操作成功-----'
        self.log.info(text)

        switch_dn = self.pri_sh.exec_cm_ctl('switchover', '-n 2',
                                            f'-D {macro.DB_INSTANCE_PATH}')
        self.log.info(switch_dn)
        self.assertIn(self.constant.cm_switchover_success_msg, switch_dn,
                      'switchover操作失败')

        time.sleep(5)
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)

        text = '-----step4:在新主节点查询页面分布信息; expect:buffer不存在-----'
        self.log.info(text)

        res2 = self.sta_sh.execut_db_sql(sql1)
        self.log.info(res2)
        self.assertIn('(2 rows)', res2, '查看表数据页面的分布信息成功')

    def tearDown(self):
        text = '-----step5:删除表，恢复集群初始状态; expect:恢复初始状态成功-----'
        self.log.info(text)

        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)

        recovery_dn = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(recovery_dn)

        time.sleep(5)
        clean = self.pri_sh.execut_db_sql(f'drop table {self.table} cascade;')
        self.log.info(clean)

        self.assertIn(self.constant.DROP_TABLE_SUCCESS, clean, '删除表失败')
        self.assertIn("cluster_state   : Normal", status, '集群状态异常')
        self.assertIn(self.constant.cm_switchover_success_msg, recovery_dn,
                      '恢复集群初始状态失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

